package com.test.socketio;

import com.corundumstudio.socketio.Configuration;
import com.corundumstudio.socketio.SocketConfig;
import com.corundumstudio.socketio.SocketIOServer;
import lombok.extern.slf4j.Slf4j;

/**
 * @author heyanjing
 * date:2020-06-18 2020/6/18:16:04
 */
@Slf4j
public class Server {

    public static void main(String[] args) {
        Configuration config = new Configuration();
        config.setPort(8337);
        final SocketConfig socketConfig = new SocketConfig();
        socketConfig.setReuseAddress(true);
        config.setSocketConfig(socketConfig);
        SocketIOServer server = new SocketIOServer(config);
        server.addConnectListener(client -> {
            log.info("{} 已连接", client.getSessionId());
        });
        server.addDisconnectListener(client -> {
            log.info("{} 已断开连接", client.getSessionId());
        });
        server.addEventListener("clientMsg", String.class, (client, data, ackSender) -> {
            log.info("{} {}", client.getSessionId(), data);
            server.getBroadcastOperations().sendEvent("serverMsg", "服务端发送消息");
        });
        server.start();
    }

}
